package com.qsl.ssyx.acl.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsl.ssyx.acl.service.AdminService;
import com.qsl.ssyx.acl.service.RoleService;
import com.qsl.ssyx.common.result.Result;
import com.qsl.ssyx.common.utils.MD5;
import com.qsl.ssyx.model.acl.Admin;
import com.qsl.ssyx.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 青衫泪
 * @since 2023-08-06
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")
public class AdminController {


    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    @ApiOperation("查询所有角色 和根据用户id已分配的角色")
    @GetMapping("toAssign/{adminId}")
    public Result toAssign(@ApiParam("用户Id") @PathVariable Integer adminId) {

        Map<String, Object> map = roleService.getRoleAll(adminId);
        return Result.ok(map);
    }

    @ApiOperation("为用户分配角色") // 先删除在新增
    @PostMapping("doAssign")
    public Result doAssign(@ApiParam("用户id") @RequestParam(required = true) Long adminId,@ApiParam("角色ids") @RequestParam(value = "roleId",required = true) Long[] roleIds) {
        Boolean flag = adminService.doAssign(adminId, roleIds);
        return Result.ok(flag);
    }

    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result selectPage(@ApiParam(value = "当前页") @PathVariable Integer page
            , @ApiParam(value = "每页记录数")
                             @PathVariable Integer limit, AdminQueryVo adminQueryVo) {
//        System.out.println("========测试热部署======>>");
        Page<Admin> pageParam = new Page<>(page, limit);
        IPage<Admin> model = adminService.selectPage(pageParam, adminQueryVo);
        return Result.ok(model);
    }

    @ApiOperation("新增")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin) {
        // 判断是否为空
        if (!StringUtils.isEmpty(admin.getPassword())) {
            // 密码加密
            String PasswordMD5 = MD5.encrypt(admin.getPassword());
            admin.setPassword(PasswordMD5);

            boolean flag = adminService.save(admin);
            if (flag) {
                return Result.ok(flag);
            } else {
                return Result.fail(flag);
            }
        }
        return Result.fail("请输入密码");
    }

    @ApiOperation("根据Id查询")
    @PostMapping("getById/{Id}")
    public Result save(@ApiParam(value = "Id") @PathVariable Integer Id) {
        Admin admin = adminService.getById(Id);
        return Result.ok(admin);
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin) {
        boolean flag = adminService.updateById(admin);
        return Result.ok(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("remove/{Id}")
    public Result remove(@PathVariable Integer Id) {
        boolean flag = adminService.removeById(Id);
        return Result.ok(flag);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Integer> idList) {
        boolean flag = adminService.removeByIds(idList);
        return Result.ok(idList);
    }
}

